﻿/*
 * Created by SharpDevelop.
 * User: Michael
 * Date: 13/11/2013
 * Time: 6:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;

namespace AdvGenContact.Data.Serialization
{
	/// <summary>
	/// Description of GobalData.
	/// </summary>
	public static class GobalData
	{
		public static IList<IGroup> Groups {get;set;}
		public static IList<IPerson> People {get;set;}
		// You can modify salt and password for you, this is only an example

		
		
		public static void NewData(){
			Groups = new List<IGroup>();
			People = new List<IPerson>();
		}
		public  static string Open(string fileName) {
			string ret = string.Empty;
		

				
			
					var bytes =  File.ReadAllBytes(fileName);
					
						System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new BinaryFormatter();
						MemoryStream memoryStream = new MemoryStream(bytes);
						Wrapper wrapper = (Wrapper)formatter.Deserialize(memoryStream);
						People = wrapper.People;
						Groups = wrapper.Groups;
			
			return ret;
		}
		
	
		public static string SaveAs(string file)
		{
			string ret = string.Empty;
			using (var fileStream = File.OpenWrite(file))
				using (var memoryStream = new MemoryStream())
			{
				
				Wrapper wrapper = new Wrapper();
				wrapper.People = People;
				wrapper.Groups = Groups;
				var formatter = new BinaryFormatter();
				formatter.Serialize(memoryStream, wrapper);

				
				memoryStream.Seek(0, SeekOrigin.Begin);

				
				var bytes = new byte[memoryStream.Length];
				memoryStream.Read(bytes, 0, (int)memoryStream.Length);

				
			
				fileStream.Write(bytes, 0, bytes.Length);
			}
			return ret;
		}
	}
	
	
}
